package com.wythe.mall.adapter;

import android.content.Context;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wythe.mall.R;
import com.wythe.mall.model.OrderBean;
import com.wythe.mall.utils.StringUtils;
import com.wythe.mall.utils.TimeUtil;

import java.util.ArrayList;
import java.util.List;

public class StoreOrderListAdapter extends RecyclerView.Adapter<StoreOrderListAdapter.ViewHolder> {

    private List<OrderBean> arrayList;
    private List<OrderBean.ItemVoBean> mItemLists;
    private OnItemClickListener onItemClickListener;
    private Context mContext;

    public StoreOrderListAdapter(Context context, List<OrderBean> arrayList) {
        mContext = context;
        this.arrayList = arrayList;
        this.onItemClickListener = null;
    }

    public void setDatas(List<OrderBean> list) {
        arrayList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final OrderBean orderBean = arrayList.get(position);
        int status = orderBean.getStatus();
        holder.storeNameTextView.setText(TimeUtil.msToDate(orderBean.getTimeStamp(), "yyyy-MM-dd HH:mm:ss"));
        holder.storeDescTextView.setText(getStatus(status));
        String methed = (orderBean.getType() == 1) ? "收货方式：自提" : "收货方式：配送";
        holder.methedTextView.setText(methed);
        String pay = "<font color=\"#585858\">总价￥" + orderBean.getTotalAmount() + "</font>  实付：" + orderBean.getTotalAmount();
        holder.totalTextView.setText(Html.fromHtml(pay));
        GoodsOrderListAdapter goodsOrderListAdapter = new GoodsOrderListAdapter(mContext, orderBean.getItemVo());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        holder.mainRecyclerView.setLayoutManager(linearLayoutManager);//设置布局管理器
        holder.mainRecyclerView.setAdapter(goodsOrderListAdapter);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onClick(position,orderBean);
                }
            }
        });
        if (status == 5) {
            holder.mBottomButton.setVisibility(View.VISIBLE);
            holder.operaTextView.setVisibility(View.VISIBLE);
            holder.optionTextView.setVisibility(View.INVISIBLE);
            holder.operaTextView.setText("确认收货");
            holder.operaTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onConfirmClick(position, orderBean);
                    }
                }
            });
        } else if (status == 1) {
            holder.mBottomButton.setVisibility(View.VISIBLE);
            holder.operaTextView.setVisibility(View.VISIBLE);
            holder.optionTextView.setVisibility(View.VISIBLE);
            holder.operaTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onPayClick(position, orderBean);
                    }
                }
            });

            holder.optionTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onCancelClick(position, orderBean);
                    }
                }
            });
        } else if (status == 7) {
            holder.mBottomButton.setVisibility(View.VISIBLE);
            holder.operaTextView.setVisibility(View.VISIBLE);
            holder.operaTextView.setText("评价");
            holder.optionTextView.setVisibility(View.INVISIBLE);
            holder.operaTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onRecommedClick(position,orderBean);
                    }
                }
            });
        } else {
            holder.mBottomButton.setVisibility(View.GONE);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup group, int viewType) {
        View view = LayoutInflater.from(group.getContext()).inflate(R.layout.item_list_store_order, group, false);
        return new ViewHolder(view);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {

        this.onItemClickListener = listener;

    }

    public interface OnItemClickListener {
        void onClick(int position, OrderBean bean);

        void onPayClick(int position, OrderBean bean);

        void onCancelClick(int position, OrderBean bean);

        void onConfirmClick(int position, OrderBean bean);

        void onRecommedClick(int position, OrderBean bean);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private LinearLayoutCompat mainLinearLayout;
        private AppCompatTextView storeNameTextView;
        private AppCompatTextView storeDescTextView;
        private RecyclerView mainRecyclerView;
        private View zenPinView;
        private TextView totalTextView;
        private AppCompatTextView optionTextView;
        private AppCompatTextView operaTextView;
        private TextView methedTextView;
        private RelativeLayout mBottomButton;

        private ViewHolder(View itemView) {
            super(itemView);
            mainLinearLayout = (LinearLayoutCompat) itemView.findViewById(R.id.mainLinearLayout);
            storeNameTextView = (AppCompatTextView) itemView.findViewById(R.id.storeNameTextView);
            storeDescTextView = (AppCompatTextView) itemView.findViewById(R.id.storeDescTextView);
            mainRecyclerView = (RecyclerView) itemView.findViewById(R.id.mainRecyclerView);
            zenPinView = itemView.findViewById(R.id.zengPinView);
            methedTextView = itemView.findViewById(R.id.tv_methed);
            totalTextView = (TextView) itemView.findViewById(R.id.totalTextView);
            optionTextView = (AppCompatTextView) itemView.findViewById(R.id.optionTextView);
            operaTextView = (AppCompatTextView) itemView.findViewById(R.id.operaTextView);
            mBottomButton = itemView.findViewById(R.id.rl_bottom);
        }

    }

    public String getStatus(int status) {
        String str = "";
        switch (status) {
            case 1:
                str = "待支付";
                break;
            case 2:
                str = "交易失败";
                break;
            case 3:
                str = "待发货";
                break;
            case 5:
                str = "已发货";
                break;
            case 6:
                str = "待收货";
                break;
            case 7:
                str = "待评价";
                break;
            case 9:
                str = "已完成";
                break;
            case 13:
                str = "申请退货";
                break;
            case 15:
                str = "退货待审核";
                break;
            case 16:
                str = "退货待邮寄";
                break;
            case 17:
                str = "待处理退款申请";
                break;
            case 19:
                str = "退货成功";
                break;
            default:
                break;

        }
        return str;
    }

}
